<?php
defined('IN_IA') or exit('Access Denied');

class Kbwdl_nicksceneryModuleSite extends WeModuleSite {

	public function doWebAdmin() {
		require __DIR__ . '/public/admin.php';

	}

    public function doWebPlat()
    {
        global $_GPC, $_W;
		$uniacid = $_W['uniacid'];
		$admin = pdo_fetch("select * from ".tablename('knscy_system_admin')." where uniacid=:uniacid and wuid=:wuid", [
			'uniacid'=> $uniacid, 'wuid'=> $_W['user']['uid']
		]);

		if(empty($admin)) {
            $password = $_W['user']['hash'];
            $password = sha1('kbwdl_') . md5($password) . md5('_encrypt') . sha1($password);
            $password = sha1($password);
            pdo_insert('knscy_system_admin', [
                'uniacid' => $uniacid,
                'wuid' => $_W['user']['uid'],
                'username' => $_W['user']['username'] . $uniacid,
                'password' => $password,
                'head_img' => $_W['user']['avatar'],
                'status' => 1,
                'create_time' => time(),
                'update_time' => time(),
            ]);
        }
        //菜单导入
        $menus = pdo_fetchall('select * from '.tablename('knscy_system_menu') ."  where uniacid=:uniacid  ",['uniacid'=> $uniacid]);
        if(empty($menus)){
            $adapter_menus = file_get_contents(__DIR__."/public/adapter/menus.json");
            $adapter_menus = json_decode($adapter_menus, true);
            $first= [];
            foreach($adapter_menus['m1'] as $k => $item){
                    $item['uniacid'] = $uniacid;
                    $id = $item['id'];
                    unset($item['id']);
                    pdo_insert('knscy_system_menu', $item);
                    $first[$id] = pdo_insertid();
            }
            $second = [];
            foreach($first as $k => $nid){
                if(!empty($adapter_menus['m2'][$k])){
                    foreach($adapter_menus['m2'][$k] as $kj => $item){
                        $item['pid'] = $nid;
                        $item['uniacid'] = $uniacid;
                        $id = $item['id'];
                        unset($item['id']);
                        pdo_insert('knscy_system_menu', $item);
                        $second[$id] = pdo_insertid();
                    }
                }
            }
            $third = [];
            foreach($second as $k => $nid){
                //最多支持导入三级
                if(!empty($adapter_menus['m3'][$k])) {
                    foreach ($adapter_menus['m3'][$k] as $kj => $item) {
                        $item['pid'] = $nid;
                        $item['uniacid'] = $uniacid;
                        $id = $item['id'];
                        unset($item['id']);
                        pdo_insert('knscy_system_menu', $item);
                        $third[$id] = pdo_insertid();
                    }
                }
            }


        }
        //配置选项导入

        $configs = pdo_fetchall('select * from '.tablename('knscy_system_config') ."  where uniacid=:uniacid  ",['uniacid'=> $uniacid]);
        if(empty($configs)){
            $configs = file_get_contents(__DIR__."/public/adapter/config.json");
            $configs = json_decode($configs, true);
            foreach($configs as $k => $item){
                $item['uniacid'] = $uniacid;
                pdo_insert('knscy_system_config', $item);
            }
        }
        //快捷菜单选项导入
        $quicks = pdo_fetchall('select * from '.tablename('knscy_system_quick') ."  where uniacid=:uniacid  ",['uniacid'=> $uniacid]);
        if(empty($quicks)){
            $quicks =  file_get_contents(__DIR__."/public/adapter/quicks.json");
            $quicks = json_decode($quicks, true);
            foreach($quicks as $k => $item){
                $item['uniacid'] = $uniacid;
                pdo_insert('knscy_system_quick', $item);
            }
        }
        //把配置信息
        foreach($configs as $k => $item)
        {
           $configs[$item['name']] = $item;
        }
        include $this->template("plat");

    }


    public function doMobileIndex(){
        global $_GPC, $_W;

$kbwdl_nickscenery_siteinfo = <<<EOD

    window.siteinfo = {
            "uniacid": "{$_W['uniacid']}",
            "acid": "{$_W['acid']}",
            "multiid": "1",
            "version": "1.8.1",
            "siteroot": "{$_W['siteroot']}app/index.php",
            'method_design': '3',
            'session_id': "{$_W['session_id']}",
            'openid': "gzhc_{$_W['openid']}"
    }; 

EOD;
        include $this->template("index");
    }

}